﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows.Media;
using Zove.Runtime;

namespace WPF3DTutorial.Model {
    public class SquarePyramidVisualizer : AbstractModelVisualizer {

        public SquarePyramidVisualizer(Window1 window)
            : base(window) {
            SolutionFactory factory = new SolutionFactory();
            this.solution = factory.CreateSolution("SquarePyramid");
        }

        public override Model3DGroup Draw() {
            RuntimeFactor rfLength 
                = this.solution.Model.GetFactorByName("edge");
            double length = Convert.ToDouble(rfLength.Value);

            RuntimeFactor rfHeight 
                = this.solution.Model.GetFactorByName("height");
            double height = Convert.ToDouble(rfHeight.Value);

            Point3D a, b, c, d, e;

            double x = 0, y = 0, z = 0;
            Point3D m, n;
            a = new Point3D(x, y, z);
            b = new Point3D(x, y + length, z);
            c = new Point3D(x + length, y + length, 0);
            d = new Point3D(x + length, y, z);
            e = new Point3D((x + length) / 2, (y + length) / 2, z + height);
            m = new Point3D((x + length) / 2, (y + length) / 2, 0);
            n = new Point3D(length, (y + length) / 2, 0);

            MeshGeometry3D mesh = new MeshGeometry3D();
            mesh.Positions.Add(a);
            mesh.Positions.Add(b);
            mesh.Positions.Add(c);
            mesh.Positions.Add(d);
            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);
            mesh.TriangleIndices.Add(3);

            Vector3D normal = window.CalculateNormal(a, b, c);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            Material material = new DiffuseMaterial(
                new SolidColorBrush(Colors.DarkKhaki));
            //ScreenSpaceLines3D line = new ScreenSpaceLines3D();
            //line.Points.Add(n);
            //line.Points.Add(e);
            //this.mainViewport.Children.Add(line);

            GeometryModel3D model = new GeometryModel3D(
                mesh, material);

            Model3DGroup group = new Model3DGroup();
            //group.Children.Add(model);

            // base
            group.Children.Add(window.CreateTriangleModel(a, b, c, Colors.Black));
            group.Children.Add(window.CreateTriangleModel(c, d, a, Colors.Orange));

            // slant faces
            group.Children.Add(window.CreateTriangleModel(e, a, b, Colors.Blue));
            group.Children.Add(window.CreateTriangleModel(a, d, e, Colors.Blue));
            group.Children.Add(window.CreateTriangleModel(c, b, e, Colors.Blue));
            group.Children.Add(window.CreateTriangleModel(d, c, e, Colors.Blue));

            group.Children.Add(window.CreateTriangleModel(m, b, e, Colors.Red));
            group.Children.Add(window.CreateTriangleModel(n, m, e, Colors.Salmon));

            return group;
        }
    }
}
